package com.canin.product.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.struts2.interceptor.SessionAware;

import com.canin.Constant;
import com.canin.dao.BreedDAO;
import com.canin.dao.DAOFactory;
import com.canin.dao.FoodDAO;
import com.canin.model.Breed;
import com.canin.model.Food;
import com.canin.model.FoodFTN;
import com.canin.model.SizeWarning;
import com.canin.util.CaninUtil;
import com.opensymphony.xwork2.ActionSupport;

public class FoodAction extends ActionSupport  implements SessionAware{

	private static final long serialVersionUID = 1954591024241616596L;
	
	private Map<String, Object> userSession;
	private Food food = new Food();
	
	private List<Food> foodList = new ArrayList<Food>();
	private List<Food> foodSelectedList = new ArrayList<Food>();
	
	private Map<String, String> selectBrand = null;
	private Map<String, String> selectType = CaninUtil.initializeFoodType();
	private Map<Integer, String> selectBreedId = null;
	private Map<String, String> selectSize = CaninUtil.initializeDogSizes();
	private Map<String, String> selectLifeSpan = CaninUtil.initializeLifeSpan();
	private Map<String, String> selectSizeType = CaninUtil.initializeSizeCounts();
	private Map<String, String> sizeType = null;
	private Map<String, String> calType = null;
	private Map<Integer, String> foodFTNTypes = new TreeMap<Integer, String>();
	private Map<Integer, String> foodFTNSubTypes1 = new TreeMap<Integer, String>();
	private Map<Integer, String> foodFTNSubTypes2 = new TreeMap<Integer, String>();
	private Map<Integer, String> foodFTNSubTypes3 = new TreeMap<Integer, String>();
	private Map<Integer, String> foodFTNSubTypes4 = new TreeMap<Integer, String>();
	private Map<Integer, String> foodFTNSubTypes5 = new TreeMap<Integer, String>();

	private ArrayList<SizeWarning> sizeWarningList = new ArrayList<SizeWarning>();
	
	private Map<String, String> breedNameList = null;
	private Map<String, String> tastes = null;
	
	double averagePricePerKL1 = 0;
	double averagePricePerKL2 = 0;
	double averagePricePerKL3 = 0;
	double averagePricePerKL4 = 0;
	double averagePricePerKL5 = 0;

	double calSize1 = 0;
	double calSize2 = 0;
	double calSize3 = 0;
	double calSize4 = 0;
	double calSize5 = 0;
	
	public String main()
	{
		return "main";		
	}
	
	public String get() throws Exception
	{
		DAOFactory daoFactory = new DAOFactory();
		daoFactory.beginConnection();

		FoodDAO foodDAO = daoFactory.createFoodDAO();
		foodList = foodDAO.getAllFood();
		
		BreedDAO breedDAO = daoFactory.createBreedDAO();
		for ( Food food : foodList )
		{
			// breedOrSize
			if ( food.getDogBreedId() != 0 )
			{
				Breed breed = breedDAO.getBreedById( food.getDogBreedId() );
				food.setBreedOrSize( breed.getBreedName() );
			}
			else if ( !"".equals( food.getDogSize() ) )
			{
				food.setBreedOrSize( selectSize.get(food.getDogSize()) );
			}
			
			// lifeSpan
			if ( !"".equals( food.getDogAge() ) )
			{
				food.setLifeSpan( selectLifeSpan.get( food.getDogAge() ) );
			}
			
			// type
			food.setType( selectType.get( food.getFoodType() ) );
		}
		
		return "get";
	}
	
	public String toSearch() throws Exception
	{
		DAOFactory daoFactory = new DAOFactory();
		daoFactory.beginConnection();
		FoodDAO foodDAO = daoFactory.createFoodDAO();

		selectBrand = foodDAO.getAllFoodBrand();
		selectBreedId = foodDAO.getAllDogBreedId();
		
		return "toSearch";
	}
	
	public String search() throws Exception
	{
		DAOFactory daoFactory = new DAOFactory();
		daoFactory.beginConnection();
		FoodDAO foodDAO = daoFactory.createFoodDAO();

		selectBrand = foodDAO.getAllFoodBrand();
		selectBreedId = foodDAO.getAllDogBreedId();
		
		//--
		foodSelectedList = foodDAO.getSelectedFood( food );
		BreedDAO breedDAO = daoFactory.createBreedDAO();
		for ( Food food : foodSelectedList )
		{
			// breedOrSize
			if ( food.getDogBreedId() != 0 )
			{
				Breed breed = breedDAO.getBreedById( food.getDogBreedId() );
				food.setBreedOrSize( breed.getBreedName() );
			}
			else if ( !"".equals( food.getDogSize() ) )
			{
				food.setBreedOrSize( selectSize.get(food.getDogSize()) );
			}
			
			// lifeSpan
			if ( !"".equals( food.getDogAge() ) )
			{
				food.setLifeSpan( selectLifeSpan.get( food.getDogAge() ) );
			}
			
			// type
			food.setType( selectType.get( food.getFoodType() ) );
		}
		
		return "toSearch";
	}
	
	public String toAdd() throws Exception
	{
		initialValueForAdd1();
		
		return "add1";
	}
	
	/**
	 * Add typical for food
	 */
	public String add1() throws Exception
	{
		initialValueForAdd1();
		
		// Check Required fields
		if ( !"".equals( food.getFoodName() )
			&& ( !"-1".equals( food.getFoodBrand()) || !"".equals( food.getNewFoodBrand() ) ) 
			&& !"".equals( food.getDescription() )
			&& ( food.getKilocalorie() != 0 && !"-1".equals( food.getCalType() ) )
			&& ( food.getDogBreedId() != -1 || !"-1".equals( food.getDogSize() ) )
			&& !"-1".equals( food.getFoodType() )
			&& !"-1".equals( food.getDogAge() ) )
		{
			// Food Brand
			if ( "-1".equals( food.getFoodBrand() ) )
			{
				food.setFoodBrand( food.getNewFoodBrand() );
			}
			
			// Taste
			if ( "-1".equals( food.getTaste() ) )
			{
				food.setTaste( food.getNewTaste() );
			}
			else if ( "".equals( food.getNewTaste() ) )
			{
				food.setTaste( "" );
			}
			
			// Breed or Size
			if ( food.getDogBreedId() == -1 || food.getDogBreedId() == 0 )
			{
				food.setDogBreedId( 0 );
			}
			else
			{
				food.setDogSize( "" );
			}
			
			if ( "-1".equals( food.getDogSize() ) )
			{
				food.setDogSize( "" );
			}
			
			DAOFactory daoFactory = new DAOFactory();
			daoFactory.beginConnection();
			FoodDAO foodDAO = daoFactory.createFoodDAO();
			
			foodDAO.addNewFood1(food);

			// thaiDogBreedName
			if ( food.getDogBreedId() != 0 )
			{
				BreedDAO breedDAO = daoFactory.createBreedDAO();
				Breed breed = breedDAO.getBreedById( food.getDogBreedId() );
				food.setThaiDogBreedName( breed.getBreedName() );
			}
			
			// thaiDogSize
			if ( !"".equals( food.getDogSize() ) )
			{
				food.setThaiDogSize( selectSize.get(food.getDogSize()) );
			}
			
			food.setThaiCalType( calType.get( food.getCalType() ) );
			food.setLifeSpan( selectLifeSpan.get( food.getDogAge() ) );
			food.setType( selectType.get( food.getFoodType() ) );
			
			food.setId( foodDAO.getLastFoodInserted() );
			
			userSession.put(Constant.FOOD, food);
//			addActionMessage("เพิ่มข้อมูลทั่วไปของ " + food.getFoodName() + "เรียบร้อย " + food.getId());
		}
		else
		{
			addActionMessage("กรุณากรอกข้อมูลให้ครบถ้วน");
			return "add1";
		}
		
		return "add2";
	}
	
	/**
	 * Add ingredient for food
	 */
	public String add2() throws Exception
	{
		if ( !"".equals( food.getIngredient() ) )
		{
			String ingredient = food.getIngredient().toLowerCase();
			
			food = (Food)userSession.get( Constant.FOOD );
			food.setIngredient( ingredient );
			
			// Check ingredient
			
			if ( ingredient.contains( "beef" ) || ingredient.contains( "วัว" ) ){
				food.setBeefIngredient(1);
			}else{
				food.setBeefIngredient(0);
			}
			
			if ( ingredient.contains( "pork" ) || ingredient.contains( "หมู" ) ){
				food.setPorkIngredient(1);
			}else{
				food.setPorkIngredient(0);
			}
			
			if ( ingredient.contains( "chicken" ) || ingredient.contains( "ไก่" ) ){
				food.setChickenIngredient(1);
			}else{
				food.setChickenIngredient(0);
			}
			
			if ( ingredient.contains( "fish" ) || ingredient.contains( "ปลา" ) ){
				food.setFishIngredient(1);
			}else{
				food.setFishIngredient(0);
			}
			
			if ( ingredient.contains( "milk" ) || ingredient.contains( "นม" ) ){
				food.setMilkIngredient(1);
			}else{
				food.setMilkIngredient(0);
			}
			
			if ( ingredient.contains( "whey" ) || ingredient.contains( "หางนม" ) ){
				food.setWheyIngredient(1);
			}else{
				food.setWheyIngredient(0);
			}
			
			if ( ingredient.contains( "egg" ) || ingredient.contains( "ไข่" ) ){
				food.setEggIngredient(1);
			}else{
				food.setEggIngredient(0);
			}
			
			if ( ingredient.contains( "soybean" ) || ingredient.contains( "ถั่วเหลือง" ) ){
				food.setSoybeanIngredient(1);
			}else{
				food.setSoybeanIngredient(0);
			}
			
			if ( ingredient.contains( "corn" ) || ingredient.contains( "ข้าวโพด" ) ){
				food.setCornIngredient(1);
			}else{
				food.setCornIngredient(0);
			}
			
			DAOFactory daoFactory = new DAOFactory();
			daoFactory.beginConnection();
			FoodDAO foodDAO = daoFactory.createFoodDAO();
			
			foodDAO.updateIngredient(food);
			
			List<FoodFTN> foodFTNTypeList = new ArrayList<FoodFTN>();
			foodFTNTypeList = foodDAO.getFoodFTNTypeAll();
			for ( FoodFTN foodFtn : foodFTNTypeList )
			{
				foodFTNTypes.put(foodFtn.getTypeId(), foodFtn.getTypeName());
			}
			
			userSession.put(Constant.FOOD, food);
		}
		else
		{
			addActionMessage("กรุณากรอกข้อมูลส่วนประกอบที่สำคัญ");
			return "add2";
		}
		
		
		
		return "add3";
	}
	
	/**
	 * Add food ftn
	 */
	public String ftn() throws Exception
	{
		int ftnType1 = food.getFoodFTNType1();
		int ftnSubType1 = food.getFoodFTNSubType1();
		int ftnType2 = food.getFoodFTNType2();
		int ftnSubType2 = food.getFoodFTNSubType2();
		int ftnType3 = food.getFoodFTNType3();
		int ftnSubType3 = food.getFoodFTNSubType3();
		int ftnType4 = food.getFoodFTNType4();
		int ftnSubType4 = food.getFoodFTNSubType4();
		int ftnType5 = food.getFoodFTNType5();
		int ftnSubType5 = food.getFoodFTNSubType5();
		
		Food food = (Food)userSession.get( Constant.FOOD );
		
		DAOFactory daoFactory = new DAOFactory();
		daoFactory.beginConnection();
		FoodDAO foodDAO = daoFactory.createFoodDAO();
		
		List<FoodFTN> foodFTNTypeList = new ArrayList<FoodFTN>();
		foodFTNTypeList = foodDAO.getFoodFTNTypeAll();
		for ( FoodFTN foodFtn : foodFTNTypeList )
		{
			foodFTNTypes.put(foodFtn.getTypeId(), foodFtn.getTypeName());
		}
		
		// ftn1
		if ( ftnType1 != -1 && food != null  )
		{
			ArrayList<FoodFTN> foodFTNSubTypeList = foodDAO.getFoodFTNSubType( ftnType1 );
			for ( FoodFTN foodFtn : foodFTNSubTypeList )
			{
				foodFTNSubTypes1.put(foodFtn.getFtnId(), foodFtn.getTargetName());
			}
			
			if ( ftnSubType1 != -1 )
			{
				food.setFoodFTNSubTypeName1( foodDAO.getFoodFTNSubTypeName( ftnSubType1 ) );
//				addActionMessage("Ready to add ftn1!");
			}
			else
			{
				food.setFoodFTNSubTypeName1( "" );
			}
			
			food.setFoodFTNType1( ftnType1 );
			food.setFoodFTNSubType1( ftnSubType1 );
			food.setFoodFTNSubTypeName1( food.getFoodFTNSubTypeName1() );
		}
		else
		{
			food.setFoodFTNType1( ftnType1 );
			foodFTNSubTypes1 = new TreeMap<Integer, String>();
		}
		
		// ftn2
		if ( ftnType2 != -1 && food != null  )
		{
			ArrayList<FoodFTN> foodFTNSubTypeList = foodDAO.getFoodFTNSubType( ftnType2 );
			for ( FoodFTN foodFtn : foodFTNSubTypeList )
			{
				foodFTNSubTypes2.put(foodFtn.getFtnId(), foodFtn.getTargetName());
			}
			
			if ( ftnSubType2 != -1 )
			{
				food.setFoodFTNSubTypeName2( foodDAO.getFoodFTNSubTypeName( ftnSubType2 ) );
//				addActionMessage("Ready to add ftn2!");
			}
			else
			{
				food.setFoodFTNSubTypeName2( "" );
			}
			
			food.setFoodFTNType2( ftnType2 );
			food.setFoodFTNSubType2( ftnSubType2 );
			food.setFoodFTNSubTypeName2( food.getFoodFTNSubTypeName2() );
		}
		else
		{
			food.setFoodFTNType2( ftnType2 );
			foodFTNSubTypes2 = new TreeMap<Integer, String>();
		}
		
		// ftn3
		if ( ftnType3 != -1 && food != null  )
		{
			ArrayList<FoodFTN> foodFTNSubTypeList = foodDAO.getFoodFTNSubType( ftnType3 );
			for ( FoodFTN foodFtn : foodFTNSubTypeList )
			{
				foodFTNSubTypes3.put(foodFtn.getFtnId(), foodFtn.getTargetName());
			}
			
			if ( ftnSubType3 != -1 )
			{
				food.setFoodFTNSubTypeName3( foodDAO.getFoodFTNSubTypeName( ftnSubType3 ) );
//				addActionMessage("Ready to add ftn3!");
			}
			else
			{
				food.setFoodFTNSubTypeName3( "" );
			}
			
			food.setFoodFTNType3( ftnType3 );
			food.setFoodFTNSubType3( ftnSubType3 );
			food.setFoodFTNSubTypeName3( food.getFoodFTNSubTypeName3() );
		}
		else
		{
			food.setFoodFTNType3( ftnType3 );
			foodFTNSubTypes3 = new TreeMap<Integer, String>();
		}
		
		// ftn4
		if ( ftnType4 != -1 && food != null  )
		{
			ArrayList<FoodFTN> foodFTNSubTypeList = foodDAO.getFoodFTNSubType( ftnType4 );
			for ( FoodFTN foodFtn : foodFTNSubTypeList )
			{
				foodFTNSubTypes4.put(foodFtn.getFtnId(), foodFtn.getTargetName());
			}
			
			if ( ftnSubType4 != -1 )
			{
				food.setFoodFTNSubTypeName4( foodDAO.getFoodFTNSubTypeName( ftnSubType4 ) );
//				addActionMessage("Ready to add ftn4!");
			}
			else
			{
				food.setFoodFTNSubTypeName4( "" );
			}
			
			food.setFoodFTNType4( ftnType4 );
			food.setFoodFTNSubType4( ftnSubType4 );
			food.setFoodFTNSubTypeName4( food.getFoodFTNSubTypeName4() );
		}
		else
		{
			food.setFoodFTNType4( ftnType4 );
			foodFTNSubTypes4 = new TreeMap<Integer, String>();
		}
		
		// ftn5
		if ( ftnType5 != -1 && food != null  )
		{
			ArrayList<FoodFTN> foodFTNSubTypeList = foodDAO.getFoodFTNSubType( ftnType5 );
			for ( FoodFTN foodFtn : foodFTNSubTypeList )
			{
				foodFTNSubTypes5.put(foodFtn.getFtnId(), foodFtn.getTargetName());
			}
			
			if ( ftnSubType5 != -1 )
			{
				food.setFoodFTNSubTypeName5( foodDAO.getFoodFTNSubTypeName( ftnSubType5 ) );
//				addActionMessage("Ready to add ftn5!");
			}
			else
			{
				food.setFoodFTNSubTypeName5( "" );
			}
			
			food.setFoodFTNType5( ftnType5 );
			food.setFoodFTNSubType5( ftnSubType5 );
			food.setFoodFTNSubTypeName5( food.getFoodFTNSubTypeName5() );
		}
		else
		{
			food.setFoodFTNType5( ftnType5 );
			foodFTNSubTypes5 = new TreeMap<Integer, String>();
		}
		
		userSession.put(Constant.FOOD, food);
		
		return "add3";
	}
	
	/**
	 * Add food ftn
	 */
	public String add3() throws Exception
	{
		Food food = (Food)userSession.get( Constant.FOOD );
		
		int ftnType1 = food.getFoodFTNType1();
		int ftnSubType1 = food.getFoodFTNSubType1();
		int ftnType2 = food.getFoodFTNType2();
		int ftnSubType2 = food.getFoodFTNSubType2();
		int ftnType3 = food.getFoodFTNType3();
		int ftnSubType3 = food.getFoodFTNSubType3();
		int ftnType4 = food.getFoodFTNType4();
		int ftnSubType4 = food.getFoodFTNSubType4();
		int ftnType5 = food.getFoodFTNType5();
		int ftnSubType5 = food.getFoodFTNSubType5();
		
		DAOFactory daoFactory = new DAOFactory();
		daoFactory.beginConnection();
		FoodDAO foodDAO = daoFactory.createFoodDAO();
		
		List<FoodFTN> foodFTNTypeList = new ArrayList<FoodFTN>();
		foodFTNTypeList = foodDAO.getFoodFTNTypeAll();
		for ( FoodFTN foodFtn : foodFTNTypeList )
		{
			foodFTNTypes.put(foodFtn.getTypeId(), foodFtn.getTypeName());
		}
		
		// ftn1
		if ( ftnType1 != -1 && food != null  )
		{
			ArrayList<FoodFTN> foodFTNSubTypeList = foodDAO.getFoodFTNSubType( ftnType1 );
			for ( FoodFTN foodFtn : foodFTNSubTypeList )
			{
				foodFTNSubTypes1.put(foodFtn.getFtnId(), foodFtn.getTargetName());
			}
			
			if ( ftnSubType1 != -1 )
			{
				food.setFoodFTNSubTypeName1( foodDAO.getFoodFTNSubTypeName( ftnSubType1 ) );
//				addActionMessage("Ready to add ftn1!");
			}
			else
			{
				food.setFoodFTNSubTypeName1( "" );
			}
			
			food.setFoodFTNType1( ftnType1 );
			food.setFoodFTNSubType1( ftnSubType1 );
			food.setFoodFTNSubTypeName1( food.getFoodFTNSubTypeName1() );
		}
		else
		{
			foodFTNSubTypes1 = new TreeMap<Integer, String>();
		}
		
		// ftn2
		if ( ftnType2 != -1 && food != null  )
		{
			ArrayList<FoodFTN> foodFTNSubTypeList = foodDAO.getFoodFTNSubType( ftnType2 );
			for ( FoodFTN foodFtn : foodFTNSubTypeList )
			{
				foodFTNSubTypes2.put(foodFtn.getFtnId(), foodFtn.getTargetName());
			}
			
			if ( ftnSubType2 != -1 )
			{
				food.setFoodFTNSubTypeName2( foodDAO.getFoodFTNSubTypeName( ftnSubType2 ) );
//				addActionMessage("Ready to add ftn2!");
			}
			else
			{
				food.setFoodFTNSubTypeName2( "" );
			}
			
			food.setFoodFTNType2( ftnType2 );
			food.setFoodFTNSubType2( ftnSubType2 );
			food.setFoodFTNSubTypeName2( food.getFoodFTNSubTypeName2() );
		}
		else
		{
			foodFTNSubTypes2 = new TreeMap<Integer, String>();
		}
		
		// ftn3
		if ( ftnType3 != -1 && food != null  )
		{
			ArrayList<FoodFTN> foodFTNSubTypeList = foodDAO.getFoodFTNSubType( ftnType3 );
			for ( FoodFTN foodFtn : foodFTNSubTypeList )
			{
				foodFTNSubTypes3.put(foodFtn.getFtnId(), foodFtn.getTargetName());
			}
			
			if ( ftnSubType3 != -1 )
			{
				food.setFoodFTNSubTypeName3( foodDAO.getFoodFTNSubTypeName( ftnSubType3 ) );
//				addActionMessage("Ready to add ftn3!");
			}
			else
			{
				food.setFoodFTNSubTypeName3( "" );
			}
			
			food.setFoodFTNType3( ftnType3 );
			food.setFoodFTNSubType3( ftnSubType3 );
			food.setFoodFTNSubTypeName3( food.getFoodFTNSubTypeName3() );
		}
		else
		{
			foodFTNSubTypes3 = new TreeMap<Integer, String>();
		}
		
		// ftn4
		if ( ftnType4 != -1 && food != null  )
		{
			ArrayList<FoodFTN> foodFTNSubTypeList = foodDAO.getFoodFTNSubType( ftnType4 );
			for ( FoodFTN foodFtn : foodFTNSubTypeList )
			{
				foodFTNSubTypes4.put(foodFtn.getFtnId(), foodFtn.getTargetName());
			}
			
			if ( ftnSubType4 != -1 )
			{
				food.setFoodFTNSubTypeName4( foodDAO.getFoodFTNSubTypeName( ftnSubType4 ) );
//				addActionMessage("Ready to add ftn4!");
			}
			else
			{
				food.setFoodFTNSubTypeName4( "" );
			}
			
			food.setFoodFTNType4( ftnType4 );
			food.setFoodFTNSubType4( ftnSubType4 );
			food.setFoodFTNSubTypeName4( food.getFoodFTNSubTypeName4() );
		}
		else
		{
			foodFTNSubTypes4 = new TreeMap<Integer, String>();
		}
		
		// ftn5
		if ( ftnType5 != -1 && food != null  )
		{
			ArrayList<FoodFTN> foodFTNSubTypeList = foodDAO.getFoodFTNSubType( ftnType5 );
			for ( FoodFTN foodFtn : foodFTNSubTypeList )
			{
				foodFTNSubTypes5.put(foodFtn.getFtnId(), foodFtn.getTargetName());
			}
			
			if ( ftnSubType5 != -1 )
			{
				food.setFoodFTNSubTypeName5( foodDAO.getFoodFTNSubTypeName( ftnSubType5 ) );
//				addActionMessage("Ready to add ftn5!");
			}
			else
			{
				food.setFoodFTNSubTypeName5( "" );
			}
			
			food.setFoodFTNType5( ftnType5 );
			food.setFoodFTNSubType5( ftnSubType5 );
			food.setFoodFTNSubTypeName5( food.getFoodFTNSubTypeName5() );
		}
		else
		{
			foodFTNSubTypes5 = new TreeMap<Integer, String>();
		}
		
		boolean add = false;
		if( ( ( ftnType1 != -1 && ftnSubType1 != -1 ) || ( ftnType1 == -1 && ftnSubType1 == -1 ) || ftnType1 != 0 || ftnSubType1 != 0 )
			&& ( ( ftnType2 != -1 && ftnSubType2 != -1 ) || ( ftnType2 == -1 && ftnSubType2 == -1 ) || ftnType2 != 0 || ftnSubType2 != 0 )
			&& ( ( ftnType3 != -1 && ftnSubType3 != -1 ) || ( ftnType3 == -1 && ftnSubType3 == -1 ) || ftnType3 != 0 || ftnSubType3 != 0 )
			&& ( ( ftnType4 != -1 && ftnSubType4 != -1 ) || ( ftnType4 == -1 && ftnSubType4 == -1 ) || ftnType4 != 0 || ftnSubType4 != 0 )
			&& ( ( ftnType5 != -1 && ftnSubType5 != -1 ) || ( ftnType5 == -1 && ftnSubType5 == -1 ) || ftnType5 != 0 || ftnSubType5 != 0 ) ) 
		{
//			addActionMessage("Ready to add ftn!");
			if ( ftnType1 != -1 && ftnSubType1 != -1 && ftnType1 != 0 && ftnSubType1 != 0 )
			{
				foodDAO.addFTN( food.getId(), ftnSubType1 );
				add = true;
			}
			if ( ftnType2 != -1 && ftnSubType2 != -1 && ftnType2 != 0 && ftnSubType2 != 0 )
			{
				foodDAO.addFTN( food.getId(), ftnSubType2 );
				add = true;
			}
			if ( ftnType3 != -1 && ftnSubType3 != -1 && ftnType3 != 0 && ftnSubType3 != 0 )
			{
				foodDAO.addFTN( food.getId(), ftnSubType3 );
				add = true;
			}
			if ( ftnType4 != -1 && ftnSubType4 != -1 && ftnType4 != 0 && ftnSubType4 != 0 )
			{
				foodDAO.addFTN( food.getId(), ftnSubType4 );
				add = true;
			}
			if ( ftnType5 != -1 && ftnSubType5 != -1 && ftnType5 != 0 && ftnSubType5 != 0 )
			{
				foodDAO.addFTN( food.getId(), ftnSubType5 );
				add = true;
			}
		}
		else
		{
			addActionMessage("กรุณากรอกข้อมูลให้ครบถ้วน");
			return "add3";
		}
		
		if ( add )
		{
//			addActionMessage("เพิ่มข้อมูลสูตรอาหารเรียบร้อย"+food.getId());
		}
		else
		{
			addActionMessage("อาหารนี้ไม่มีสูตร");
		}
		
		userSession.put(Constant.FOOD, food);
		
		return "add4";
	}
	
	/**
	 * Add food size&price
	 */
	public String add4() throws Exception
	{
		Food foodTmp = (Food)userSession.get( Constant.FOOD );
		
		if ( food.getSize1() != 0.0 && !"".equals(food.getSizeType1()) && food.getPrice1() != 0 )
		{
			calSize1 = food.getSize1();
			if ( Constant.SIZE_COUNT_GRAM.equals(food.getSizeType1()) )
			{
				// 1000 gram = 1 kilogram
				calSize1 = calSize1/1000;
			}
			else if ( Constant.SIZE_COUNT_ONZ.equals(food.getSizeType1()) )
			{
				// 1 onz = 28.35 gram
				calSize1 = (calSize1*28.35)/1000;
			}
			
//			foodDAO.addSizeAndPrice( foodTmp.getId(), calSize, food.getPrice1(), food.getDelivery1() );
			foodTmp.setSize1( food.getSize1() );
			foodTmp.setSizeType1(food.getSizeType1());
			foodTmp.setPrice1(food.getPrice1());
			foodTmp.setDelivery1(food.getDelivery1());

			averagePricePerKL1 = (foodTmp.getPrice1()/calSize1);
		}
		else
		{
			addActionMessage("กรุณากรอกข้อมูลให้ครบถ้วน");
		}
		
		if ( food.getSize2() != 0.0 && !"".equals(food.getSizeType2()) && food.getPrice2() != 0 )
		{
			calSize2 = food.getSize2();
			if ( Constant.SIZE_COUNT_GRAM.equals(food.getSizeType2()) )
			{
				// 1000 gram = 1 kilogram
				calSize2 = calSize2/1000;
			}
			else if ( Constant.SIZE_COUNT_ONZ.equals(food.getSizeType2()) )
			{
				// 1 onz = 28.35 gram
				calSize2 = (calSize2*28.35)/1000;
			}
			
//			foodDAO.addSizeAndPrice( foodTmp.getId(), calSize, food.getPrice2(), food.getDelivery2() );
			foodTmp.setSize2( food.getSize2() );
			foodTmp.setSizeType2(food.getSizeType2());
			foodTmp.setPrice2(food.getPrice2());
			foodTmp.setDelivery2(food.getDelivery2());

			averagePricePerKL2 = (foodTmp.getPrice2()/calSize2);
		}
		
		if ( food.getSize3() != 0.0 && !"".equals(food.getSizeType3()) && food.getPrice3() != 0 )
		{
			calSize3 = food.getSize3();
			if ( Constant.SIZE_COUNT_GRAM.equals(food.getSizeType3()) )
			{
				// 1000 gram = 1 kilogram
				calSize3 = calSize3/1000;
			}
			else if ( Constant.SIZE_COUNT_ONZ.equals(food.getSizeType3()) )
			{
				// 1 onz = 28.35 gram
				calSize3 = (calSize3*28.35)/1000;
			}
			
//			foodDAO.addSizeAndPrice( foodTmp.getId(), calSize, food.getPrice3(), food.getDelivery3() );
			foodTmp.setSize3( food.getSize3() );
			foodTmp.setSizeType3(food.getSizeType3());
			foodTmp.setPrice3(food.getPrice3());
			foodTmp.setDelivery3(food.getDelivery3());

			averagePricePerKL3 = (foodTmp.getPrice3()/calSize3);
		}

		if ( food.getSize4() != 0.0 && !"".equals(food.getSizeType4()) && food.getPrice4() != 0 )
		{
			calSize4 = food.getSize4();
			if ( Constant.SIZE_COUNT_GRAM.equals(food.getSizeType4()) )
			{
				// 1000 gram = 1 kilogram
				calSize4 = calSize4/1000;
			}
			else if ( Constant.SIZE_COUNT_ONZ.equals(food.getSizeType4()) )
			{
				// 1 onz = 28.35 gram
				calSize4 = (calSize4*28.35)/1000;
			}
			
//			foodDAO.addSizeAndPrice( foodTmp.getId(), calSize, food.getPrice4(), food.getDelivery4() );
			foodTmp.setSize4( food.getSize4() );
			foodTmp.setSizeType4(food.getSizeType4());
			foodTmp.setPrice4(food.getPrice4());
			foodTmp.setDelivery4(food.getDelivery4());

			averagePricePerKL4 = (foodTmp.getPrice4()/calSize4);
		}
		
		if ( food.getSize5() != 0.0 && !"".equals(food.getSizeType5()) && food.getPrice5() != 0 )
		{
			calSize5 = food.getSize5();
			if ( Constant.SIZE_COUNT_GRAM.equals(food.getSizeType5()) )
			{
				// 1000 gram = 1 kilogram
				calSize5 = calSize5/1000;
			}
			else if ( Constant.SIZE_COUNT_ONZ.equals(food.getSizeType5()) )
			{
				// 1 onz = 28.35 gram
				calSize5 = (calSize5*28.35)/1000;
			}
			
//			foodDAO.addSizeAndPrice( foodTmp.getId(), calSize, food.getPrice5(), food.getDelivery5() );
			foodTmp.setSize5( food.getSize5() );
			foodTmp.setSizeType5(food.getSizeType5());
			foodTmp.setPrice5(food.getPrice5());
			foodTmp.setDelivery5(food.getDelivery5());

			averagePricePerKL5 = (foodTmp.getPrice5()/calSize5);
		}
		
		double dd = 0;
		SizeWarning sw = new SizeWarning();
		// 1 ==> 2,3,4,5
		if ( averagePricePerKL1 != 0 )
		{
			if ( averagePricePerKL2 != 0 ){
				if ( averagePricePerKL1 > averagePricePerKL2 ){
					dd = ( (averagePricePerKL1 - averagePricePerKL2)/averagePricePerKL1 )*100;
				}else{
					dd = ( (averagePricePerKL2 - averagePricePerKL1)/averagePricePerKL2 )*100;
				}
				
				if ( dd >= 50 ){
					sw.setFisrt(1);
					sw.setSecond(2);
					sizeWarningList.add( sw );
				}			
			}
			
			if ( averagePricePerKL3 != 0 ){
				if ( averagePricePerKL1 > averagePricePerKL3 ){
					dd = ( (averagePricePerKL1 - averagePricePerKL3)/averagePricePerKL1 )*100;
				}else{
					dd = ( (averagePricePerKL3 - averagePricePerKL1)/averagePricePerKL3 )*100;
				}
				
				if ( dd >= 50 ){
					sw.setFisrt(1);
					sw.setSecond(3);
					sizeWarningList.add( sw );
				}			
			}
			
			if ( averagePricePerKL4 != 0 ){
				if ( averagePricePerKL1 > averagePricePerKL4 ){
					dd = ( (averagePricePerKL1 - averagePricePerKL4)/averagePricePerKL1 )*100;
				}else{
					dd = ( (averagePricePerKL4 - averagePricePerKL1)/averagePricePerKL2 )*100;
				}
				
				if ( dd >= 50 ){
					sw.setFisrt(1);
					sw.setSecond(4);
					sizeWarningList.add( sw );
				}			
			}
			
			if ( averagePricePerKL5 != 0 ){
				if ( averagePricePerKL1 > averagePricePerKL5 ){
					dd = ( (averagePricePerKL1 - averagePricePerKL5)/averagePricePerKL1 )*100;
				}else{
					dd = ( (averagePricePerKL5 - averagePricePerKL1)/averagePricePerKL2 )*100;
				}
				
				if ( dd >= 50 ){
					sw.setFisrt(1);
					sw.setSecond(5);
					sizeWarningList.add( sw );
				}			
			}
		}
		
		// 2 ==> 3,4,5
		if ( averagePricePerKL2 != 0 )
		{
			if ( averagePricePerKL3 != 0 ){
				if ( averagePricePerKL2 > averagePricePerKL3 ){
					dd = ( (averagePricePerKL2 - averagePricePerKL3)/averagePricePerKL2 )*100;
				}else{
					dd = ( (averagePricePerKL3 - averagePricePerKL2)/averagePricePerKL3 )*100;
				}
				
				if ( dd >= 50 ){
					sw.setFisrt(1);
					sw.setSecond(2);
					sizeWarningList.add( sw );
				}			
			}
			
			if ( averagePricePerKL4 != 0 ){
				if ( averagePricePerKL2 > averagePricePerKL4 ){
					dd = ( (averagePricePerKL2 - averagePricePerKL4)/averagePricePerKL2 )*100;
				}else{
					dd = ( (averagePricePerKL4 - averagePricePerKL2)/averagePricePerKL4 )*100;
				}
				
				if ( dd >= 50 ){
					sw.setFisrt(1);
					sw.setSecond(3);
					sizeWarningList.add( sw );
				}			
			}
			
			if ( averagePricePerKL5 != 0 ){
				if ( averagePricePerKL2 > averagePricePerKL5 ){
					dd = ( (averagePricePerKL2 - averagePricePerKL5)/averagePricePerKL2 )*100;
				}else{
					dd = ( (averagePricePerKL5 - averagePricePerKL2)/averagePricePerKL2 )*100;
				}
				
				if ( dd >= 50 ){
					sw.setFisrt(1);
					sw.setSecond(5);
					sizeWarningList.add( sw );
				}			
			}
		}
		
		// 3 ==> 4,5
		if ( averagePricePerKL3 != 0 )
		{
			if ( averagePricePerKL4 != 0 ){
				if ( averagePricePerKL3 > averagePricePerKL4 ){
					dd = ( (averagePricePerKL3 - averagePricePerKL4)/averagePricePerKL3 )*100;
				}else{
					dd = ( (averagePricePerKL4 - averagePricePerKL3)/averagePricePerKL4 )*100;
				}
				
				if ( dd >= 50 ){
					sw.setFisrt(1);
					sw.setSecond(3);
					sizeWarningList.add( sw );
				}			
			}
			
			if ( averagePricePerKL5 != 0 ){
				if ( averagePricePerKL3 > averagePricePerKL5 ){
					dd = ( (averagePricePerKL3 - averagePricePerKL5)/averagePricePerKL3 )*100;
				}else{
					dd = ( (averagePricePerKL5 - averagePricePerKL3)/averagePricePerKL3 )*100;
				}
				
				if ( dd >= 50 ){
					sw.setFisrt(1);
					sw.setSecond(5);
					sizeWarningList.add( sw );
				}			
			}
		}
		
		// 4 ==> 5
		if ( averagePricePerKL4 != 0 )
		{
			if ( averagePricePerKL5 != 0 ){
				if ( averagePricePerKL4 > averagePricePerKL5 ){
					dd = ( (averagePricePerKL4 - averagePricePerKL5)/averagePricePerKL4 )*100;
				}else{
					dd = ( (averagePricePerKL5 - averagePricePerKL4)/averagePricePerKL4 )*100;
				}
				
				if ( dd >= 50 ){
					sw.setFisrt(1);
					sw.setSecond(5);
					sizeWarningList.add( sw );
				}			
			}
		}
		
		if ( sizeWarningList.size() != 0 )
		{
			// show warning message
			String warningMessage = "";
			for ( SizeWarning sizeW : sizeWarningList )
			{
				warningMessage = warningMessage + "ขนาดและราคาของ " + sizeW.getFisrt() 
						+ "และ " + sizeW.getSecond() + " ไม่สัมพันธ์กัน\n";
			}
			
			addActionMessage( warningMessage );
		}
		else
		{
			return "added";
		}
		
		food = foodTmp;
		userSession.put(Constant.FOOD, food);
		
		return "add4";
	}
	
	public String added() throws Exception
	{
		addFoodSizeAndPrice();
		
		return "added";
	}
	
	private void addFoodSizeAndPrice() throws Exception
	{
		// ready to add
		Food foodTmp = (Food)userSession.get( Constant.FOOD );
		
		DAOFactory daoFactory = new DAOFactory();
		daoFactory.beginConnection();
		FoodDAO foodDAO = daoFactory.createFoodDAO();
		
		// Add Callories
		if ( Constant.KILOCALORIES.equals(food.getCalType()) )
		{
			foodDAO.updateCallories( foodTmp.getId(), food.getKilocalorie() );
		}
		else if ( Constant.CALORIES_PER_CUP.equals(food.getCalType()) )
		{
			// 1 cup = 100 grams
			foodDAO.updateCallories( foodTmp.getId(), food.getKilocalorie()*10 );
		}
		else if ( Constant.CALORIES_PER_PACKAGE.equals(food.getCalType()) )
		{
			// Only 1 size
			if ( Constant.SIZE_COUNT_KILOGRAM.equals(food.getSizeType1()) )
			{
				foodDAO.updateCallories( foodTmp.getId(), food.getKilocalorie()*food.getSize1() );
			}
			else if ( Constant.SIZE_COUNT_GRAM.equals(food.getSizeType1()) )
			{
				// 1000 gram = 1 kilogram
				foodDAO.updateCallories( foodTmp.getId(), food.getKilocalorie()*(1000/food.getSize1()) );
			}
			else if ( Constant.SIZE_COUNT_ONZ.equals(food.getSizeType1()) )
			{
				// 1 onz = 28.35 gram
				foodDAO.updateCallories( foodTmp.getId(), food.getKilocalorie()*(1000/(food.getSize1()*28.35)) );
			}
		}
		
		if ( averagePricePerKL5 != 0 ){
			foodDAO.addSizeAndPrice( foodTmp.getId(), calSize5, food.getPrice5(), food.getDelivery5() );
		}
		if ( averagePricePerKL4 != 0 ){
			foodDAO.addSizeAndPrice( foodTmp.getId(), calSize4, food.getPrice4(), food.getDelivery4() );
		}
		if ( averagePricePerKL3 != 0 ){
			foodDAO.addSizeAndPrice( foodTmp.getId(), calSize3, food.getPrice3(), food.getDelivery3() );
		}
		if ( averagePricePerKL2 != 0 ){
			foodDAO.addSizeAndPrice( foodTmp.getId(), calSize2, food.getPrice2(), food.getDelivery2() );
		}
		
		if ( averagePricePerKL1 != 0 ){
			foodDAO.addSizeAndPrice( foodTmp.getId(), calSize1, food.getPrice1(), food.getDelivery1() );
			addActionMessage("เพิ่มข้อมูลอาหารสุนัขเรียบร้อย");
		}
	}
	
	private void initialValueForAdd1() throws Exception
	{
		calType = CaninUtil.initializeCalType();
		
		DAOFactory daoFactory = new DAOFactory();
		daoFactory.beginConnection();
		FoodDAO foodDAO = daoFactory.createFoodDAO();

		selectBrand = foodDAO.getAllFoodBrand();
		breedNameList = CaninUtil.retrieveBreedNameList();
		tastes = foodDAO.getAllFoodTaste();
	}
	
	@Override
	public void setSession(Map<String, Object> userSession)
	{
		this.userSession = userSession;
	}

	public Map<String, Object> getUserSession()
	{
		return userSession;
	}

	public void setUserSession(Map<String, Object> userSession)
	{
		this.userSession = userSession;
	}

	public Food getFood()
	{
		return food;
	}

	public void setFood(Food food)
	{
		this.food = food;
	}

	public List<Food> getFoodList()
	{
		return foodList;
	}

	public void setFoodList(List<Food> foodList)
	{
		this.foodList = foodList;
	}

	public Map<String, String> getSelectBrand()
	{
		return selectBrand;
	}

	public void setSelectBrand(Map<String, String> selectBrand)
	{
		this.selectBrand = selectBrand;
	}

	public Map<String, String> getSelectType()
	{
		return selectType;
	}

	public void setSelectType(Map<String, String> selectType)
	{
		this.selectType = selectType;
	}

	public Map<Integer, String> getSelectBreedId()
	{
		return selectBreedId;
	}

	public void setSelectBreedId(Map<Integer, String> selectBreedId)
	{
		this.selectBreedId = selectBreedId;
	}

	public Map<String, String> getSelectSize()
	{
		return selectSize;
	}

	public void setSelectSize(Map<String, String> selectSize)
	{
		this.selectSize = selectSize;
	}

	public Map<String, String> getSelectLifeSpan()
	{
		return selectLifeSpan;
	}

	public void setSelectLifeSpan(Map<String, String> selectLifeSpan)
	{
		this.selectLifeSpan = selectLifeSpan;
	}

	public List<Food> getFoodSelectedList()
	{
		return foodSelectedList;
	}

	public void setFoodSelectedList(List<Food> foodSelectedList)
	{
		this.foodSelectedList = foodSelectedList;
	}

	public Map<String, String> getBreedNameList()
	{
		return breedNameList;
	}

	public void setBreedNameList(Map<String, String> breedNameList)
	{
		this.breedNameList = breedNameList;
	}

	public Map<String, String> getTastes()
	{
		return tastes;
	}

	public void setTastes(Map<String, String> tastes)
	{
		this.tastes = tastes;
	}

	public Map<String, String> getSizeType()
	{
		return sizeType;
	}

	public void setSizeType(Map<String, String> sizeType)
	{
		this.sizeType = sizeType;
	}

	public Map<String, String> getCalType()
	{
		return calType;
	}

	public void setCalType(Map<String, String> calType)
	{
		this.calType = calType;
	}

	public Map<Integer, String> getFoodFTNTypes()
	{
		return foodFTNTypes;
	}

	public void setFoodFTNTypes(Map<Integer, String> foodFTNTypes)
	{
		this.foodFTNTypes = foodFTNTypes;
	}

	public Map<Integer, String> getFoodFTNSubTypes1()
	{
		return foodFTNSubTypes1;
	}

	public void setFoodFTNSubTypes1(Map<Integer, String> foodFTNSubTypes1)
	{
		this.foodFTNSubTypes1 = foodFTNSubTypes1;
	}

	public Map<Integer, String> getFoodFTNSubTypes2()
	{
		return foodFTNSubTypes2;
	}

	public void setFoodFTNSubTypes2(Map<Integer, String> foodFTNSubTypes2)
	{
		this.foodFTNSubTypes2 = foodFTNSubTypes2;
	}

	public Map<Integer, String> getFoodFTNSubTypes3()
	{
		return foodFTNSubTypes3;
	}

	public void setFoodFTNSubTypes3(Map<Integer, String> foodFTNSubTypes3)
	{
		this.foodFTNSubTypes3 = foodFTNSubTypes3;
	}

	public Map<Integer, String> getFoodFTNSubTypes4()
	{
		return foodFTNSubTypes4;
	}

	public void setFoodFTNSubTypes4(Map<Integer, String> foodFTNSubTypes4)
	{
		this.foodFTNSubTypes4 = foodFTNSubTypes4;
	}

	public Map<Integer, String> getFoodFTNSubTypes5()
	{
		return foodFTNSubTypes5;
	}

	public void setFoodFTNSubTypes5(Map<Integer, String> foodFTNSubTypes5)
	{
		this.foodFTNSubTypes5 = foodFTNSubTypes5;
	}

	public static long getSerialversionuid()
	{
		return serialVersionUID;
	}

	public Map<String, String> getSelectSizeType()
	{
		return selectSizeType;
	}

	public void setSelectSizeType(Map<String, String> selectSizeType)
	{
		this.selectSizeType = selectSizeType;
	}

}
